package example.com.zyfypt_618zgm.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import example.com.zyfypt_618zgm.R;
import example.com.zyfypt_618zgm.activity.Denglu;
import example.com.zyfypt_618zgm.adapter.MyAdapter;
import example.com.zyfypt_618zgm.bean.ModBean;
import example.com.zyfypt_618zgm.model.ModModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


import static example.com.zyfypt_618zgm.activity.Main.truemod;

/**
 * Created by 小茗同学 on 2017/6/1.
 */

public class MyModFragment extends Fragment {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager manager;
    private MyAdapter myAdapter;
    private List<ModBean> list=null;
    private String Session;
    private View view=null;
    private Context context;
    private int cur_page=1;
    private int lastVisibleItemPosition;
    private String str;
    public MyModFragment(String str){
        this.str=str;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment,container,false);
        context=getActivity();
        cur_page=1;
        recyclerView=(RecyclerView)view.findViewById(R.id.recycleview1);
        manager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        myAdapter=new MyAdapter(context);
        myAdapter.setList(list);
        recyclerView.setAdapter(myAdapter);
        truemod=str;
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    cur_page+=1;
                    truemod=str;
                    Call<List<ModBean>> call= new ModModel().getResultList(truemod, cur_page, Session);
                    call.enqueue(new Callback<List<ModBean>>() {
                        @Override
                        public void onResponse(Call<List<ModBean>> call, Response<List<ModBean>> response) {
                            if(response.isSuccessful()&&response.body()!=null)
                            {
                                list.addAll(response.body());
                                myAdapter.setList(list);
                            }
                            else
                            {
                                Toast.makeText(context,"获取资源失败",Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<List<ModBean>> call, Throwable t) {
                            Toast.makeText(context,"请检查网络连接",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=list.size()-1;
            }
        });
        ModModel modModel =new ModModel();
        Call<List<ModBean>> call= modModel.getResultList(truemod, cur_page, Denglu.SessionID);
        call.enqueue(new Callback<List<ModBean>>() {
            @Override
            public void onResponse(Call<List<ModBean>> call, Response<List<ModBean>> response) {
                if(response.isSuccessful()&&response.body()!=null)
                {
                    list=response.body();
                    myAdapter.setList(list);
                }
                else
                {
                    Toast.makeText(context,"获取资源失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<ModBean>> call, Throwable t) {
                Toast.makeText(context,"请检查网络连接",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
}
